import HttpUtils from '../HttpUtils/HttpUtils';
// 获取当前时间戳
var timestamp = Date.parse(new Date()) / 1000;
Page({
	...HttpUtils.func,
	data: {
		titOn: 'all',
		taskId: '', //任务ID 
		pageIndex: 1,
		pageItems: 10,
		totalPages: 0,
		studyInfo: [],
		contentList: [],
		title: '',
	},
	onLoad(params) {
		var title = params.title;
		var taskId = params.taskId;
		this.setData({
			"taskId": taskId,
			"title": title,
		});
		this.loadStudyInfo();
	},

	//查询学习任务详情
	loadStudyInfo: function(e) {
		var newUrl = getApp().globalData.dangjianUrl + "/study/studyInfo";
		var params = {};
		params["taskId"] = this.data.taskId + "";
		params = JSON.stringify(params);
		this.httpGet(newUrl, params, "任务详情接口调用失败").then(
			(respData) => {
				var result = respData.data.result.code;
				if (1 == result) {
					var studyInfo = respData.data.study;
					if (null != studyInfo && undefined != studyInfo) {
						var studyinfo = respData.data.study;
						var courseItems = respData.data.study.courseItems;
						courseItems = courseItems.courseItem;
						var contentList = [];
						for (var index in courseItems) {
							var content = courseItems[index];
							var json = {};
							var thumbnail;
							var imageFile = content.imgItems.imageFile;	//获取图片
							if (null != imageFile && "" != imageFile) {
								for (var j in imageFile) {
									thumbnail = imageFile[0].url;
									break;
								}
							}
							json["thumbnail"] = thumbnail;
							json["courseId"] = content.courseId;
							json["courseName"] = content.courseName;
							json["courseType"] = content.courseType;
							json["id"] = content.id;
							json["playTime"] = content.playTime;
							json["studyTime"] = content.studyTime;
							json["status"] = content.status;
							//将 秒转成 时间
							var h = Math.floor(content.totalTime / 3600) >= 10 ? Math.floor(content.totalTime / 3600) : '0' + Math.floor(content.totalTime / 3600);
							var m = Math.floor(content.totalTime / 60) >= 10 ? Math.floor(content.totalTime / 60) : '0' + Math.floor(content.totalTime / 60);
							var s = content.totalTime % 60 >= 10 ? content.totalTime % 60 : '0' + content.totalTime % 60;
							json["totalTime"] = '时长  ' + h + ':' + m + ":" + s;
							contentList.push(json);
						}

						var courseId = courseItems[0].courseId;
						this.setData({
							studyInfo: studyInfo,
							contentList: contentList,
							code: courseId
						});
					}
				} else {
					wx.alert({ title: respData.data.result.description });
				}
			}
		);
	},

	// 跳转 详情
	goshowDetail: function(evnet) {
		var taskId = evnet.currentTarget.dataset.taskid;
		var name = evnet.currentTarget.dataset.name;
		var courseId = evnet.currentTarget.dataset.courseid;
		wx.navigateTo({
			url: "../shortVideoDetail/shortVideoDetail?fromPage=task&taskId=" + taskId + '&name=' + name + '&courseId=' + courseId
		});
	},


	articleDetail(event) {  //学习
		var courseId = event.currentTarget.dataset.courseid;
		var taskId = event.currentTarget.dataset.taskid;
		var name = event.currentTarget.dataset.name;
		var title = event.currentTarget.dataset.title;
		var pageUrl = "../missionDetail/missionDetail?fromPage=task&taskId=" + taskId + '&name=' + name 
		+ '&courseId=' + courseId	+ '&title=' + title;
		wx.navigateTo({
			url: pageUrl
		})
	},


	onReachBottom: function(e) {
		var pageIndex = this.data.pageIndex + 1;
		var totalPages = this.data.totalPages;
		if (totalPages >= pageIndex) {
			this.setData({
				"pageIndex": pageIndex
			});
			this.loadTaskList();
		} else {
			this.showPageMsg("tips", "没有更多数据了!");
		}
	},
	onPullDownRefresh: function() {
		this.setData({
			pageIndex: 1,
			studyList: []
		});
		this.loadTaskList();
		wx.stopPullDownRefresh();
	}
});
